package com.pospi.pospihold.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.pospi.pospihold.database.DB;
import com.pospi.pospihold.dto.GoodsDto;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Qiyan on 2016/5/13.
 */
public class GoodsDao {
    private DB goodsDB;
    private String tablename = "goods";

    private GoodsDto dto;

    public GoodsDao(Context context) {
        goodsDB = new DB(context);//实例化数据库
    }


    /***
     * 给数据库里面添加数据
     */
    public void addGoods(List<GoodsDto> dtos) {
        SQLiteDatabase db = goodsDB.getWritableDatabase();
        clearFeedTable(db);//先删除表格里面的所有的数据
        Log.d("添加数据", "开始添加数据");
        for (int i = 0; i < dtos.size(); i++) {
            GoodsDto goodsDto = dtos.get(i);
            db.execSQL("insert into goods(code,name,category_sid,image,colorCodeShow,price,group_sid,mainPrinterSid,Sid)" +
                    "values(?,?,?,?,?,?,?,?,?)",
                    new Object[]{goodsDto.getCode(), goodsDto.getName(),
                            goodsDto.getCategory_sid(), goodsDto.getImage(),
                            goodsDto.getColorCodeShow(), goodsDto.getPrice(),
                            goodsDto.getGroup_sid(),goodsDto.getMainPrinterSid(),goodsDto.getSid()});
//            Log.i("添加一条数据", goodsDto.getCode() + goodsDto.getName() + goodsDto.getCategory_sid() + goodsDto.getImage() + goodsDto.getColorCodeShow() + goodsDto.getPrice());
        }
        db.close();
    }

    /**
     * 在添加数据之前先删除表格里面所有的数据
     *
     * @param db
     */
    public void clearFeedTable(SQLiteDatabase db) {

        String sql = "DELETE FROM " + tablename + ";";

        db.execSQL(sql);
        Log.i("删除表格数据", "删除数据成功");

        revertSeq(db);
    }

    private void revertSeq(SQLiteDatabase db) {
        String sql = "update sqlite_sequence set seq=0 where name='" + tablename + "'";
        db.execSQL(sql);


    }

    public GoodsDto selectGoods(String code) {
        SQLiteDatabase db = goodsDB.getReadableDatabase();
        Cursor cursor = db.query(tablename, new String[]{"name", "price"}, "code=?", new String[]{code}, null, null, null);
        if (cursor.moveToFirst()) {
            dto = new GoodsDto();
            //遍历Cursor对象，取出数据并打印
            dto.setName(cursor.getString(cursor.getColumnIndex("name")));
            dto.setGroup_sid(cursor.getString(cursor.getColumnIndex("group_sid")));
            dto.setPrice(cursor.getDouble(cursor.getColumnIndex("price")));
            dto.setCode(code);

            dto.setNum(1);
            dto.setDiscount(0);

            //打印数据
            Log.d("查询数据", "名字" + dto.getName());
            Log.d("查询数据", "价格" + dto.getPrice());
        } else {
            dto = null;
        }
        cursor.close();
        db.close();
        return dto;
    }
    public GoodsDto selectGoodsBySid(String code) {
        SQLiteDatabase db = goodsDB.getReadableDatabase();
        Cursor cursor = db.query(tablename, new String[]{"name", "price"}, "Sid=?", new String[]{code}, null, null, null);
        if (cursor.moveToFirst()) {
            dto = new GoodsDto();
            //遍历Cursor对象，取出数据并打印
            dto.setName(cursor.getString(cursor.getColumnIndex("name")));
            dto.setGroup_sid(cursor.getString(cursor.getColumnIndex("group_sid")));
            dto.setPrice(cursor.getDouble(cursor.getColumnIndex("price")));
            dto.setCode(code);

            dto.setNum(1);
            dto.setDiscount(0);

            //打印数据
            Log.d("查询数据", "名字" + dto.getName());
            Log.d("查询数据", "价格" + dto.getPrice());
        } else {
            dto = null;
        }
        cursor.close();
        db.close();
        return dto;
    }

    public List<GoodsDto> findSelectPointGoods(String category_sid) {

        List<GoodsDto> listgoodsdto = new ArrayList<>();
        SQLiteDatabase db = goodsDB.getReadableDatabase();

        Cursor cursor = db.query(tablename, null, "category_sid=?", new String[]{category_sid}, null, null, null);
        while (cursor.moveToNext()) {
            GoodsDto dto1 = new GoodsDto();
            dto1.setNum(1);
            dto1.setCode(cursor.getString(cursor.getColumnIndex("code")));
            dto1.setName(cursor.getString(cursor.getColumnIndex("name")));
            dto1.setImage(cursor.getString(cursor.getColumnIndex("image")));
            dto1.setColorCodeShow(cursor.getString(cursor.getColumnIndex("colorCodeShow")));
            dto1.setPrice(cursor.getDouble(cursor.getColumnIndex("price")));
            dto1.setGroup_sid(cursor.getString(cursor.getColumnIndex("group_sid")));
            dto1.setDiscount(cursor.getDouble(cursor.getColumnIndex("discount")));
            dto1.setSid(cursor.getString(cursor.getColumnIndex("Sid")));
            dto1.setMainPrinterSid(cursor.getString(cursor.getColumnIndex("mainPrinterSid")));
            listgoodsdto.add(dto1);
        }
        cursor.close();
        db.close();
        return listgoodsdto;
    }
}


